<?php
class core_modules {
	static protected $modlist;
	
	static function LoadModules() {
		$table = new SQL(array('M' => 'modules','C' => 'class'));
		$table->where(array(
			'modules' => 
				array(
					'active' => 'yes',
				),
			'class' =>
				array(
					'Type' => 'mod',
					'SyName' => array('field' => 'M.SyName'),
				)
		));
		$mods = $table->select();
		foreach ($mods as $mod)
		{
			if ($mod->active == 'yes')
			{
				self::$modlist[$mod->Class] = array(
					'sysName' => $mod->SyName,
					'instance' => null,
					'Name' => $mod->Name,
					'ClassName' => $mod->ClassName,
				);
				
				$libs = explode('|', $mod->libraries);
				foreach ($libs as $lib)
					if ($lib && !core_libraries::IsLoaded($lib))
						core_libraries::LoadLibrary($lib);
				
				if ($mod->autoStart == 'yes')
					self::$modlist[$mod->Class]['instance'] = new $mod->Class ();
			}
		}
	}
	
	static function FindFile($file)
	{
		$ret = array();
		foreach (self::$modlist as $class => $mod)
		{
			$pathfile = MODSDIR . $mod['sysName'] . DIRECTORY_SEPARATOR . $file;
			if (file_exists($pathfile))
			{
				$ret[] = $class;
			}
		}
		return $ret;
	}
	
	static function ActivedMods()
	{
		return array_keys(self::$modlist);
	}
	
	static function LoadUnregistredMod($file)
	{
		$content = file_get_contents($file);
		preg_match('`class +(mod_[a-z0-1]+)`i', $content, $m);
		unset($content);

		$sysName = '';
		if (stripos(dirname($file), COREDIR))
			$sysName = str_ireplace(COREDIR, '..' . DIRECTORY_SEPARATOR, dirname($file));

		require_once $file;
		
		self::$modlist[$m[1]] = array(
			'instance' => new $m[1](),
			'Name' => substr($m[1], 4),
			'ClassName' => substr($m[1], 4),
			'sysName' => $sysName,
		);
	}
	
	static function FindClass($className)
	{
		foreach (self::$modlist as $class => $module)
		{
			if ($module['ClassName'] == $className)
				return $class;
		}
		return false;
	}
	
	static function modDir($module = null)
	{
		if ($module == null)
			$module = LastClass();

		$module = 'mod' . strchr($module, '_');

		if (isset(self::$modlist[$module]) && self::$modlist[$module])
		{
			return self::$modlist[$module]['sysName'];
		}
		else
		{
			return false;
		}
	}
	
	static function IsLoaded($module)
	{
		return isset(self::$modlist[$module]);
	}
	
	static function &GetDetail($module)
	{
		if (isset(self::$modlist[$module]))
		{
			return self::$modlist[$module];
		}
		else
			throw new ModuleLoadingError ('Module don\'t exist.');
	}
	
	static function &GetInstance($module)
	{
		if (!self::IsLoaded($module))
		{
			var_dump($module);
			throw new ModuleLoadingError ('Module don\'t exist.');
		}
		else
		{
			if (self::$modlist[$module]['instance'] === null)
				self::$modlist[$module]['instance'] = new $module();
			return self::$modlist[$module]['instance'];
		}
	}

	static function ModMap($args, $func, $static = true)
	{
		$ret = array();
		foreach (self::$modlist as $class => $mod)
		{
			if ($static)
			{
				$callback = array($class, $func);
			}
			else
			{
				$callback = array($mod['instance'], $func);
			}
			
			if (is_callable($callback))
			{
				$ret[$class] = call_user_func_array(array($mod['instance'], $func), $args);
			}
			else
			{
				$ret[$class] = null;
			}
		}
		return $ret;
	}
	
	static function IterYes($args, $func = 'UrlCatcher', $static = true)
	{
		$mod = self::ModMap($args, $func, $static);
		return array_keys($mod, true);
	}
}
